Part of the Regex Documentation website.
Quantifiers

Quantifiers are a compact method for describing the number of times a particular match is allowed to occur.

Quantifiers always apply to the item they are immediately following. Alternatively stated, they apply to the immediately-preceding item in the regular expression, including back-references, and grouped sub-patterns.

The Match-zero-or-more metacharacter (*)

The "*" metacharacter means match any number, including zero, of the immediately preceding item in the regular expression. That is, repeat the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern.

Example
"< *H1 *>" matches both "<H1>" and "<   H1 >" - both of which are valid HTML statements.

Since the "*" metacharacter allows zero matches - then "x*" will always match something - even if it is the empty space between the commencement of the string and the first character.

Example
regex.easysearch ("x*", "This old man", @temp.matchinfo)
   » true - if you examine the matchinfo table, the match commences at position 1, but has zero length.

regex.easysearch ("x*", "xxxxxxxxx", @temp.matchinfo)
   » true

The Match-one-or-more metacharacter (+)

The "+" metacharacter matches only if the preceding character occurs at least once in the target string (along with the rest of the stated pattern).

Examples
regex.easysearch ("ca+r", "car", @temp.matchinfo)
   » true

regex.easysearch ("ca+r", "caaaar", @temp.matchinfo)
   » true

regex.easysearch ("ca+r", "cr", @temp.matchinfo)
   » false

The Match-zero-or-one metacharacter (?)

The "?" metacharacter means match either zero or one of the immediately preceding characters. It is similar to the match-zero-or-more operator except that it repeats the preceding regular expression once or not at all.

Examples
regex.easysearch ("ca?r", "car", @temp.matchinfo)
   » true

regex.easysearch ("ca?r", "caaaar", @temp.matchinfo)
   » false

regex.easysearch ("ca?r", "cr", @temp.matchinfo)
   » true

The Match-a specified-number of times Interval ({min, max})

Intervals allow you to specify exactly the minimum and maximum times a match of the immediately preceding character is allowed to occur.

"{COUNT}" matches exactly COUNT occurrences of the preceding regular expression.

"{MIN,}" matches MIN or more occurrences of the preceding regular expression.

"{MIN, MAX}" matches at least MIN but no more than MAX occurrences of the preceding regular expression

Example
"ab{1,3}" matches "ab", "abb", "abbb", but neither "a" nor "abbbb".


This page was last updated at Sun, 08 Nov 1998 16:07:13 GMT.
Please send all questions and comments to regex@lists.scriptmeridian.org.
Check our website for updates to the docs.